package pipeline;

import baseObjects.Symbol;
import java.math.BigInteger;
import java.util.Properties;

public class KeyboardCandidate {

    
    private BigInteger id;
    //private Vector<myKey> keys;
    private Symbol[][] keys = null;
    private KeyboardKey[][] keymap = null;
    private double score=0.0;

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    private KeyboardCandidate() {
        
    }

    public KeyboardCandidate(KeyboardKey[] keymap) {
        this();
        this.keymap = new KeyboardKey[4][];
        this.keys = new Symbol[4][];
        this.keymap[0] = new KeyboardKey[13];
        this.keys[0] = new Symbol[13];
        this.keymap[1] = new KeyboardKey[13];
        this.keys[1] = new Symbol[13];
        this.keymap[2] = new KeyboardKey[11];
        this.keys[2] = new Symbol[11];
        this.keymap[3] = new KeyboardKey[10];
        this.keys[3] = new Symbol[10];
        int i = 0, j = 0;
        for (int row = 0; row < 4; row++) {
            for (i = 0; i < this.keymap[row].length; i++) {
                this.keymap[row][i] = new KeyboardKey(keymap[j++]);
                this.keys[row][i] = this.keymap[row][i].getSymbol();
            }
        }
//        System.out.println("KEYcANDI");
//        for(i=0; i<4; i++){
//            for(j=0; j<this.keymap[i].length; j++)
//                System.out.print(this.keymap[i][j]+"|");
//            System.out.println("");
//        }
    }

    public KeyboardCandidate(KeyboardKey[][] option){
        this();
        this.keymap = new KeyboardKey[option.length][];
        this.keys = new Symbol[option.length][];
        this.keymap[0] = new KeyboardKey[option[0].length];
        this.keys[0] = new Symbol[option[0].length];
        this.keymap[1] = new KeyboardKey[option[1].length];
        this.keys[1] = new Symbol[option[1].length];
        this.keymap[2] = new KeyboardKey[option[2].length];
        this.keys[2] = new Symbol[option[2].length];
        this.keymap[3] = new KeyboardKey[option[3].length];
        this.keys[3] = new Symbol[option[3].length];
        for (int i=0; i< this.keymap.length; i++){
            for (int j=0; j<this.keymap[i].length; j++){
                this.keymap[i][j] = new KeyboardKey(option[i][j]);
                this.keys[i][j] = option[i][j].getSymbol();
            }
        }
    }


    public Symbol[][] getKeys() {
        return keys;
    }

    //stub
    public Properties getKeysP() {
        return null;
    }

    public void setKeys(Symbol[][] keys) {
        this.keys = keys;
    }

    /**
     * @return the id
     */
    public BigInteger getId() {
        return id;
    }

    public void setID(BigInteger i){
        this.id = i;
    }

    public int getKeyCode(Character c) {
        int ans = 0;
        boolean found = false;
        for (int i = 0; i < keys.length && !found; i++) {
            for (int j = 0; j < keys[i].length && !found; j++) {
                int code = i*100 + j+1;
                if (keys[i][j].equals(c)) {
                    Symbol s = keys[i][j];
                    if (s.getRegular().equals(c)){
                        ans =code;
                    }
                    else if (s.getShift()!= null && s.getShift().equals(c)){
                        ans = 1000 + code;
                    } else if (s.getCtrl()!=null && s.getCtrl().equals(c)){
                        ans = 2000 + code;
                    } else if (s.getAlt()!= null && s.getAlt().equals(c)){
                        ans= 3000 + code;
                    }
                    found = true;
                }
            }
        }

        return ans;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(System.getProperty("line.separator")+"|");
        for (int i = 0; i < keys.length; i++) {
            for (int j = 0; j < keys[i].length; j++) {
                Symbol symbol = keys[i][j];
                sb.append((i*100+j+1)+":"+symbol.toString()+"|");
            }
            sb.append(System.getProperty("line.separator"));
        }

        return sb.toString();
    }



}
